package Lee_code;

import java.util.ArrayList;
import java.util.List;

/**
 * @author blh
 * @date 2024年08月11日 16:34
 */
public class Lee_131_811 {
    List<List<String>> res = new ArrayList<>();
    int len;

    public List<List<String>> partition(String s) {
        len = s.length();
        char[] chars = s.toCharArray();
        dfs(0, new ArrayList<String>(), s);
        return res;
    }

    private void dfs(int index, List<String> curStr, String s) {
        if (index == len) {
            res.add(new ArrayList<>(curStr));
            return;
        }
        for (int i = index; i < len; i++) {
            String tempStr = s.substring(index, i + 1);
            if (judgeHuiwen(tempStr)) {
                curStr.add(tempStr);
                dfs(i + 1, curStr, s);
                curStr.remove(curStr.size() - 1);
            }
        }
    }

    boolean judgeHuiwen(String s) {
        if (s.length() == 1) {
            return true;
        }
        int left = 0, right = s.length() - 1;
        while (left < right) {
            if (s.charAt(left) != s.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}
